package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.product.entity.BaseAttrInfo;
import com.atguigu.gmall.product.entity.BaseAttrValue;
import com.atguigu.gmall.product.service.BaseAttrInfoService;
import com.atguigu.gmall.product.service.BaseAttrValueService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 */
@Slf4j
@RequestMapping("/admin/product")
@RestController
@Api(value = "AttrController", description = "手机参数管理接口", tags = {"手机管理"})
public class AttrController {

    @Autowired
    BaseAttrInfoService baseAttrInfoService;

    @Autowired
    BaseAttrValueService baseAttrValueService;

    /**
     * 查询某个平台属性的所有属性值
     * @param attrId
     * @return
     */
    @GetMapping("/getAttrValueList/{attrId}")
    public Result getAttrValueList(@PathVariable("attrId")Long attrId){
        List<BaseAttrValue> values = baseAttrValueService.getAttrValueList(attrId);
        return Result.ok(values);
    }


    /**
     * 查询分类下的平台属性
     * @return
     */
    @GetMapping("/attrInfoList/{categoryId1}/{categoryId2}/{categoryId3}")
    public Result attrInfoList(@PathVariable("categoryId1") Long categoryId1,
                               @PathVariable("categoryId2") Long categoryId2,
                               @PathVariable("categoryId3") Long categoryId3){
        //数据从哪里来？接到前端的数据
        //数据怎么处理？定义service功能，进行处理【处理逻辑交给service】
        //数据给哪里去？处理数据并返回
        List<BaseAttrInfo> result = baseAttrInfoService
                .getAttrsAndValueByCategory(categoryId1,categoryId2,categoryId3);
        return Result.ok(result);
    }

    /**
     * 保存属性
     * @param baseAttrInfo
     * @return
     */

    @PostMapping("/saveAttrInfo")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        log.info("保存/新增属性： {}",baseAttrInfo);
        baseAttrInfoService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }

}
